﻿
using Microsoft.AspNetCore.Http;
using Microsoft.Net.Http.Headers;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using UAParser;

namespace Cms.Core.Infrastructure.Common
{
   public class WebClientInfoProvider: IWebClientInfoProvider
    {

        private readonly IHttpContextAccessor _httpContextAccessor = null;
        public WebClientInfoProvider(IHttpContextAccessor httpContextAccessor)
        {
            _httpContextAccessor = httpContextAccessor;
        }
     
        public string BrowserInfo => GetBrowserInfo();


        public string ComputerName => GetComputerName();
        public string ClientIpAddress => GetClientIpAddress();

   

        public HttpContext Current => _httpContextAccessor.HttpContext;


        /// <summary>
        /// 得到计算机名
        /// </summary>
        /// <returns></returns>
        public string GetComputerName()
        {
            var username = System.Net.Dns.GetHostEntry(Current.Connection.RemoteIpAddress).HostName;
            return username;
        }

        /// <summary>
        /// 得到浏览器信息
        /// </summary>
        /// <returns></returns>
        private string GetBrowserInfo()
        {
            var userAgent = Current.Request.Headers[HeaderNames.UserAgent];
            if (userAgent.ToString() == null)
            {
                return null;
            }
            string uaString = Convert.ToString(userAgent[0]);
            var uaParser = Parser.GetDefault();

            ClientInfo c = uaParser?.Parse(uaString);
            //Other
            var device = (object)c?.Device;
            var os = (object)c?.OS;
   
            var ua = (object)c?.UserAgent;
            return $"{os} {(device.Equals(" Other ") ? string.Empty : device)} {ua}";

        }

        /// <summary>
        /// 得到IP地址
        /// </summary>
        /// <returns></returns>

        private string GetClientIpAddress()
        {
            var httpContext = Current;
            var remoteIpAddress = httpContext?.Connection.RemoteIpAddress;
            if (remoteIpAddress == null)
            {
                return null;
            }

            var clientIp = remoteIpAddress.ToString();

            try
            {
                foreach (var hostAddress in Dns.GetHostAddresses(clientIp))
                {
                    if (hostAddress.AddressFamily == AddressFamily.InterNetwork)
                    {
                        return hostAddress.ToString();
                    }
                }

                foreach (var hostAddress in Dns.GetHostAddresses(Dns.GetHostName()))
                {
                    if (hostAddress.AddressFamily == AddressFamily.InterNetwork)
                    {
                        return hostAddress.ToString();
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return clientIp;
        }

    }
}
